#include "FileTest.h"
#include <stdio.h>
#include <string.h>

/*
 * Created: 2/27/12; CGP
 
a) Create a new file, call it X (opened for writing)
b) Create a second (different) new file, call it Y (opened for writing)
c) Write multiple bytes of data into file, X
d) Write multiple bytes of data into file, Y
e) Write multiple bytes of data into file, X
f) Write multiple bytes of data into file, Y
g) Close both files
h) Open both files for reading
i) Read multiple bytes of data from file, X
j) Read multiple bytes of data from file, Y
k) Read multiple bytes of data from file, X
l) Read multiple bytes of data from file, Y
m) Verify that all the data read was the same as expected.
n) In both files, make sure that when you read past the end of the file, you can't read more data. (i.e., End of File is detected correctly).
 */

#define NUMBER_WRITES 38

int main() {
    int length;
    printf("Starting test...\n"); fflush(stdout);
    FileTest ft;
    const char *fileName1 = "X";
    const char *fileName2 = "Y"; 

    File *f1 = new File(fileName1, "w");
    File *f2 = new File(fileName2, "w");
    unsigned char data1[] = "abcdefghijklmnopqrstuvwxyz";
    unsigned char data2[] = "0123456789";
    int dataLength1 = strlen((const char *) data1)+1;
    int dataLength2 = strlen((const char *) data2)+1;

    for (int i=0; i < NUMBER_WRITES; i++) { // 38*26 bytes = 988 bytes
        length = f1->Write(data1, dataLength1);
        if (length != dataLength1) {
            printf("ERROR: Could not write all the data\n");
        }

        length = f2->Write(data2, dataLength2);
        if (length != dataLength2) {
            printf("ERROR: Could not write all the data\n");
        }
    }
    if (! f1->Close()) {
        printf("ERROR: Could not close file\n");
    }
    if (! f2->Close()) {
        printf("ERROR: Could not close file\n");
    }
    delete f1;
    delete f2;

    f1 = new File(fileName1, "r");
    f2 = new File(fileName2, "r");  
    unsigned char buffer[100];
    for (int i=0; i < NUMBER_WRITES; i++) { // 38*26 bytes = 988 bytes
        length = f1->Read(buffer, dataLength1);
        
        if (length != dataLength1) {
            printf("ERROR: Could not read back all %d bytes of file\n", dataLength1);
        }
        
        if (strcmp((const char *) data1, (const char *) buffer) != 0) {
            printf("ERROR: Buffer (= %s) is not equal to data written (= %s)\n", buffer, data1);
        }

        length = f2->Read(buffer, dataLength2);
        
        if (length != dataLength2) {
            printf("ERROR: Could not read back all %d bytes of file\n", dataLength2);
        }
        
        if (strcmp((const char *) data2, (const char *) buffer) != 0) {
            printf("ERROR: Buffer (= %s) is not equal to data written (= %s)\n", buffer, data2);
        }
    }

    length = f1->Read(buffer, 1);
    if (length != 0) {
        printf("ERROR: End of file not correctly reached\n");
    }

    length = f2->Read(buffer, 1);
    if (length != 0) {
        printf("ERROR: End of file not correctly reached\n");
    }

    if (! f1->Close()) {
        printf("ERROR: Could not close file\n");
    }
    if (! f2->Close()) {
        printf("ERROR: Could not close file\n");
    }
    delete f1;
    delete f2;      
}




